package leetcode.code0043;

public class Solution extends Solution43 {

	@Override
	public String multiply(String num1, String num2) {
		if (num1.equals("0") || num2.equals("0"))
			return "0";
		char[] n1 = num1.toCharArray();
		char[] n2 = num2.toCharArray();
		int len = n1.length + n2.length;
		int[] mult = new int[len];
		int pos = 1;
		for (int i = n1.length - 1; i >= 0; i--) {
			int lastPos = len - pos++;// 当前要处理的最后一位
			for (int j = n2.length - 1; j >= 0; j--) {
				mult[lastPos] += (n1[i] - '0') * (n2[j] - '0');// 最后一位乘积, 累加之前的结果
				mult[lastPos - 1] += mult[lastPos] / 10;// 整体向前进位
				mult[lastPos] = mult[lastPos--] % 10;// 留下最后一位的个位数
			}
		}
		StringBuffer ans = new StringBuffer();
		boolean start = false;
		for (int n : mult) {
			if (n != 0) {
				start = true;
			}
			if (start) {
				ans.append(n);
			}
		}
		return ans.toString();
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
//		System.out.println(9999 * 9999);
	}

}
